--链接阿里云物联网套件   通过MQTT方式和云交互

module(...,package.seeall)

require"aLiYun"
require"misc"
require"pm"
require"Config"
require"nvm"
require "net"
require "mqtt"
require "LBS"
--阿里云客户端是否处于连接状态
local sConnected
--上报消息计数器
local Mess_Count=1
--报警事件计数器
local Pub_Alarm_Count=0
--需要上报属性的标志位
local Pub_property_Count=0

nvm.init("Config.lua")  --初始化文件系统

--库函数的要求，写的一个转换子函数
local function getDeviceName()
    return misc.getImei()
end
--库函数的要求，写的一个转换子函数
local function getDeviceSecret()
    return nvm.get("DeviceSecret")
end

--基站定位成功后上报一次位置信息
function Pub_Location(longitude,latitude)
    if sConnected then  --只有链接成功才执行上报任务
        --经纬度属性上报
        local Propety_TOPIC = "/sys/"..nvm.get("ProductKey").."/"..getDeviceName().."/thing/event/property/post"
        local PayLoad = {}
        local RGB = CH455G.Read_RGB()
        PayLoad.id = Mess_Count
        PayLoad.version = "1.0"
        PayLoad.method = "thing.event.property.post"
        local Params = {}
        local GeoLocation = {}
        GeoLocation.Longitude = longitude
        GeoLocation.Latitude = latitude
        Params.GeoLocation = GeoLocation
        Params.soilHumidity = CH455G.ReadADC()
        Params.RSSI = net.getRssi()
        Params.voltage = misc.getVbatt()
        Params.RGB1 = RGB.RGB1
        Params.RGB2 = RGB.RGB2
        Params.RGB3 = RGB.RGB3
        Params.RGB4 = RGB.RGB4
        Params.Lighteness = RGB.Lighteness
        Params.PubTime = nvm.get("PubTime")
        PayLoad.params = Params
        PayLoad = json.encode(PayLoad)
        aLiYun.publish(Propety_TOPIC,PayLoad)   --上报阿里云属性  
    end
end
--定时上报湿度等属性
local function Pub_Humi()
    LBS.reqLbsLoc() --上报一次经纬度先
    --延时准备再次上报数据
    sys.timerStart(Pub_Humi,60000*nvm.get("PubTime"))
end
--链接成功之后上报一次ICCID即可
local function Pub_IMEI()
    if sConnected then  --只有链接成功才执行上报任务
        --属性上报主题
        local Propety_TOPIC = "/sys/"..nvm.get("ProductKey").."/"..getDeviceName().."/thing/event/property/post"
        --Mess_Count = Mess_Count + 1
        local PayLoad = {}
        PayLoad.id = Mess_Count
        PayLoad.version = "1.0"
        PayLoad.method = "thing.event.property.post"
        local Params = {}
        Params.ICCID = sim.getIccid()
        Params.RSSI = net.getRssi()
        Params.voltage = misc.getVbatt()
        Params.soilHumidity = CH455G.ReadADC()
        PayLoad.params = Params
        PayLoad = json.encode(PayLoad)
        aLiYun.publish(Propety_TOPIC,PayLoad)   --上报阿里云属性  
    end
end
--- 连接结果的处理函数
local function connectCbFnc(result)
    local Property_Set_TOPIC = "/sys/"..nvm.get("ProductKey").."/"..getDeviceName().."/thing/service/property/set"
    sConnected = result
    if sConnected==true then
        aLiYun.subscribe(Property_Set_TOPIC,0) --订阅 服务器设置设备属性的主题
        log.info("connectCbFnc.0",sConnected)
        Pub_IMEI()
        Pub_Humi() --即刻上报
    end
end
--更新完毕循环时间后上报实际数值到平台
local function Pub_UpTime(TimeUp)
    if sConnected then  --只有链接成功才执行上报任务
        local Propety_TOPIC = "/sys/"..nvm.get("ProductKey").."/"..getDeviceName().."/thing/event/property/post"
        Mess_Count = Mess_Count + 1
        local PayLoad = {}
        local RGB = CH455G.Read_RGB()
        PayLoad.id = Mess_Count
        PayLoad.version = "1.0"
        PayLoad.method = "thing.event.property.post"
        local Params = {}
        local GeoLocation = {}
        Params.soilHumidity = CH455G.ReadADC()
        Params.RSSI = net.getRssi()
        Params.voltage = misc.getVbatt()
        Params.RGB1 = RGB.RGB1
        Params.RGB2 = RGB.RGB2
        Params.RGB3 = RGB.RGB3
        Params.RGB4 = RGB.RGB4
        Params.Lighteness = RGB.Lighteness
        Params.PubTime = TimeUp
        PayLoad.params = Params
        PayLoad = json.encode(PayLoad)
        aLiYun.publish(Propety_TOPIC,PayLoad)   --上报阿里云属性
    end
end
-- 下行消息的处理
function CheckCode200(topic,qos,payload)
    local Property_Set_TOPIC = "/sys/"..nvm.get("ProductKey").."/"..getDeviceName().."/thing/service/property/set"
    local tjsondata,result,errinfo = json.decode(payload)
    local Property = 0
    --属性上报后的返回 服务器会返回循环间隔和循环次数
    if result and topic==Property_Set_TOPIC then
        --log.info("CheckCode200.0",Property)

        Property = tjsondata["params"]["PubTime"]
        if Property ~= nil then
        --如果接收到的是PubTime，接收的数据赶紧存储到Flash
            nvm.set("PubTime",tjsondata["params"]["PubTime"])
            nvm.flush()
            Pub_UpTime(nvm.get("PubTime"))
        end

        Property = tjsondata["params"]["RGB1"]
        if Property ~= nil then
            CH455G.ShowRGB("LED1","R",Property["R"])
            CH455G.ShowRGB("LED1","G",Property["G"])
            CH455G.ShowRGB("LED1","B",Property["B"])
        end

        Property = tjsondata["params"]["RGB2"]
        if Property ~= nil then
            CH455G.ShowRGB("LED2","R",Property["R"])
            CH455G.ShowRGB("LED2","G",Property["G"])
            CH455G.ShowRGB("LED2","B",Property["B"])
        end

        Property = tjsondata["params"]["RGB3"]
        if Property ~= nil then
            CH455G.ShowRGB("LED3","R",Property["R"])
            CH455G.ShowRGB("LED3","G",Property["G"])
            CH455G.ShowRGB("LED3","B",Property["B"])
        end

        Property = tjsondata["params"]["RGB4"]
        if Property ~= nil then
            CH455G.ShowRGB("LED4","R",Property["R"])
            CH455G.ShowRGB("LED4","G",Property["G"])
            CH455G.ShowRGB("LED4","B",Property["B"])
        end

        Property = tjsondata["params"]["Lighteness"]
        if Property ~= nil then
            CH455G.Set_Lighteness(Property)
        end

        --接受过新的状态后，反馈最新属性到平台
        LBS.reqLbsLoc() --上报一次经纬度，顺带一起上报温湿度了

    end
end
--开机初始化设备
function Init_Device()
    --if sConnected==false then
        --配置阿里云的链接参数
        aLiYun.setMqtt(1,nil,60)
        aLiYun.setup(nvm.get("ProductKey"),nil,getDeviceName,getDeviceSecret)
        aLiYun.on("connect",connectCbFnc)
        aLiYun.on("receive",CheckCode200)
    --end
end
Init_Device()



